/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import org.balisunrise.common.Mode;
import org.balisunrise.swing.components.panel.layouts.TransactionActionLayoutBuilder;

/**
 *
 * @author Glauco
 */
public class TransactionActionPanel extends JPanel{

    private JButton searchButton;
    private JButton createButton;
    private JButton updateButton;
    private JButton deleteButton;

    private List<JButton> actions;

    private TransactionActionLayoutBuilder layoutBuilder;

    public TransactionActionPanel(JButton searchButton, JButton createButton,
            JButton updateButton, JButton deleteButton, List<JButton> actions,
            TransactionActionLayoutBuilder layoutBuilder) {

        super();

        this.searchButton = searchButton;
        this.createButton = createButton;
        this.updateButton = updateButton;
        this.deleteButton = deleteButton;
        
        if(actions == null)
            this.actions = new ArrayList<JButton>();
        else
            this.actions = actions;

        this.layoutBuilder = layoutBuilder;

        setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        setMaximumSize(new Dimension(105, 32767));
        setMinimumSize(new Dimension(105, 315));

        createLayout();
    }

    public List<JButton> getActions() {
        return actions;
    }

    public void setActions(List<JButton> actions) {
        this.actions = actions;
    }

    public JButton getCreateButton() {
        return createButton;
    }

    public void setCreateButton(JButton createButton) {
        this.createButton = createButton;
    }

    public JButton getDeleteButton() {
        return deleteButton;
    }

    public void setDeleteButton(JButton deleteButton) {
        this.deleteButton = deleteButton;
    }

    public JButton getSearchButton() {
        return searchButton;
    }

    public void setSearchButton(JButton searchButton) {
        this.searchButton = searchButton;
    }

    public JButton getUpdateButton() {
        return updateButton;
    }

    public void setUpdateButton(JButton updateButton) {
        this.updateButton = updateButton;
    }

    public void addAction(JButton button, int index) {

        if (actions.size() < index) {
            actions.add(button);
        } else if (index < 0) {
            actions.add(0, button);
        } else {
            actions.add(index, button);
        }
        createLayout();
    }

    public void selectButton(int mode){
        searchButton.setSelected(false);
        createButton.setSelected(false);
        updateButton.setSelected(false);
        deleteButton.setSelected(false);
        switch (mode){
            case Mode.SEARCH:
                searchButton.setSelected(true);
                break;
            case Mode.NEW:
                createButton.setSelected(true);
                break;
            case Mode.UPDATE:
                updateButton.setSelected(true);
                break;
            case Mode.DELETE:
                deleteButton.setSelected(true);
                break;
        }
    }

    private void createLayout() {

        layoutBuilder.setSearchButton(searchButton);
        layoutBuilder.setCreateButton(createButton);
        layoutBuilder.setUpdateButton(updateButton);
        layoutBuilder.setDeleteButton(deleteButton);
        layoutBuilder.setComponents(actions);
        layoutBuilder.buildLayout(this);
    }

}
